import { ChartComponents, ListComponents, CardComponents } from './components.js';

async function loadDashboardData() {
    const dashboardContent = document.getElementById('student-dashboard-content');
    if (!dashboardContent) {
        console.error('找不到 dashboard 内容区域');
        return;
    }

    // 显示加载状态
    const loadingSpinner = document.createElement('div');
    loadingSpinner.id = 'dashboardLoadingSpinner';
    loadingSpinner.innerHTML = `
        <div class="text-center p-5">
            <div class="spinner-border text-info" role="status">
                <span class="sr-only">加载中...</span>
            </div>
            <p class="mt-2">正在加载数据...</p>
        </div>
    `;
    dashboardContent.appendChild(loadingSpinner);

    try {
        // 获取当前用户信息
        const currentUser = getCurrentUser();
        if (!currentUser) {
            throw new Error('未获取到用户信息，请重新登录');
        }

        // 获取学生信息
        const student = await fetchApi(`/students/info?stuNo=${currentUser.username}`);
        if (!student) {
            throw new Error('获取学生信息失败');
        }
        
        // 更新个人信息
        const studentNameElement = document.getElementById('studentName');
        const studentNoElement = document.getElementById('studentNo');
        const studentClassElement = document.getElementById('studentClass');
        
        if (studentNameElement) studentNameElement.textContent = student.name;
        if (studentNoElement) studentNoElement.textContent = student.stuNo;
        if (studentClassElement) studentClassElement.textContent = student.className;

        // 获取成绩数据
        const grades = await fetchApi('/students/my-grades');
        if (!grades) {
            throw new Error('获取成绩信息失败');
        }
        
        // 使用组件更新各个部分
        CardComponents.updateRecentExamCard(grades);
        CardComponents.updateGradeStatsCard(grades);
        
        // 加载图表
        if (grades && grades.length > 0) {
            ChartComponents.loadGradeTrendChart(grades);
            ChartComponents.loadSubjectGradeChart(grades);
        } else {
            const gradeTrendChartElement = document.getElementById('gradeTrendChart');
            const subjectGradeChartElement = document.getElementById('subjectGradeChart');
            
            if (gradeTrendChartElement) {
                gradeTrendChartElement.parentElement.innerHTML = '<p class="text-center">暂无成绩数据</p>';
            }
            if (subjectGradeChartElement) {
                subjectGradeChartElement.parentElement.innerHTML = '<p class="text-center">暂无成绩数据</p>';
            }
        }
        
        // 加载最近考试列表
        await ListComponents.loadRecentExamsList(grades);

    } catch (error) {
        console.error('加载个人主页数据失败:', error);
        showStudentToast(error.message || '加载个人主页数据失败', 'danger');
    } finally {
        // 移除加载状态
        const spinner = document.getElementById('dashboardLoadingSpinner');
        if (spinner) {
            spinner.remove();
        }
    }
} 